﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.BizFacade
{
    using System;
    using System.Data;

    using Boiling.Common;
    using Boiling.DataAccess;

    /// <summary>
    /// 新鲜事封装类
    /// </summary>
    public class c_News
    {
        public String newsID;
        public String userID;
        public String userNo;
        public String userName;
        public int newsKind;//1为日志，2为活动
        public String newHostID;
        public String newsGuestID;
        public String newsTitle;
        public String newsDate;
        public String newsPointerID;   //新鲜事所指向的活动或者日志的id
        public bool newsCover;
    }

    
    public class NewsManager
    {
        /// <summary>
        /// 通过新鲜事编号来得到一个新鲜事
        /// </summary>
        /// <param name="newsid">新鲜事id</param>
        /// <returns></returns>
        public b_Friends_Newsdata GetNewsByNewsid(int newsid)
        {
            return (new b_Friends_News()).getnewsbynewsid(newsid);
        }


        /// <summary>
        /// 输入Host_User_ID(接收新鲜事的人），得到给他的全部新鲜事（已读和未读的都有）
        /// </summary>
        /// <param name="hostID">接受新鲜事的用户邮箱</param>
        /// <returns></returns>
        public b_Friends_Newsdata GetNewsByUserid(String hostID)
        {
            return (new b_Friends_News()).getnewsbyusername(hostID);
        }

        
        /// <summary>
        /// 输入Host_User_ID,得到给他的全部未读的新鲜事
        /// </summary>
        /// <param name="hostID">接受新鲜事的用户邮箱</param>
        /// <returns></returns>
        public b_Friends_Newsdata GetNewsNotReadByUserid(String hostID)
        {
            return (new b_Friends_News()).GetNewsNotReadByName(hostID);
        }


        /// <summary>
        /// 通过用户id返回未读新鲜事封装包
        /// </summary>
        /// <param name="hostID">用户id</param>
        /// <returns></returns>
        public c_News[] GetNewsNotReadByUseridReturnClass(String hostID)
        {
            int n = (new NewsManager()).CountNewsNotReadByUserid(hostID);
            c_News[] news = new c_News[n];

            b_Friends_Newsdata newsdata = GetNewsNotReadByUserid(hostID);

            for (int i = 0; i < n; i++ )
            {
                news[i] = new c_News();
                news[i].newHostID = hostID;
                news[i].newsCover = false;
                news[i].userID = newsdata.Tables[b_Friends_Newsdata.b_Friends_News_Table].Rows[i][b_Friends_Newsdata.Guest_User_ID_Field].ToString();
                news[i].newsGuestID = news[i].userID;
                news[i].userNo = (new UserSystem()).GetUserNoByUserID(news[i].userID);
                news[i].userName = (new UserSystem()).GetUserInfodatabyID(news[i].userID).userName;
                news[i].newsKind = Int32.Parse(newsdata.Tables[b_Friends_Newsdata.b_Friends_News_Table].Rows[i][b_Friends_Newsdata.News_Kind_Field].ToString());
                news[i].newsTitle = newsdata.Tables[b_Friends_Newsdata.b_Friends_News_Table].Rows[i][b_Friends_Newsdata.News_Title_Field].ToString();
                news[i].newsDate = newsdata.Tables[b_Friends_Newsdata.b_Friends_News_Table].Rows[i][b_Friends_Newsdata.News_Date_Field].ToString();
                news[i].newsPointerID = newsdata.Tables[b_Friends_Newsdata.b_Friends_News_Table].Rows[i][b_Friends_Newsdata.Diary_ID_Field].ToString();
            }

            return news;
        }


        /// <summary>
        /// 获得一个用户未读的新鲜事数目
        /// </summary>
        /// <param name="userid">用户邮箱</param>
        /// <returns>未读新鲜事数量</returns>
        public int CountNewsNotReadByUserid(String hostID)
        {
            return GetNewsNotReadByUserid(hostID).Tables[b_Friends_Newsdata.b_Friends_News_Table].Rows.Count;
        }


        /// <summary>
        /// 输入一个接收用户邮箱和一个日志，插入到新鲜事中去并标记为未读
        /// </summary>
        /// <param name="hostID">接收用户id</param>
        /// <param name="diary">日志data</param>
        /// <returns></returns>
        public bool InsertNewsofDiary(String hostID, b_Diarydata diary)
        {
            return (new b_Friends_News()).InsertNewsofDiary(hostID, diary);
        }


        /// <summary>
        /// 向所有好友添加新鲜事
        /// </summary>
        /// <param name="diary"></param>
        /// <returns></returns>
        public bool InsertNewofDiaryToAllFriends(b_Diarydata diary)
        {
            String hostID = diary.Tables[b_Diarydata.b_Diary_Table].Rows[0][b_Diarydata.User_ID_Field].ToString();
            int n = (new FriendManager()).countfriendbyID(hostID);
            if (n == 0)
                return true;
            String[] friendsID = new String[n];
            friendsID = (new FriendManager()).SearchAllFriendsbyUserIDReturnString(hostID);
            bool test = false;
            for (int i = 0; i < n; i++ )
            {
                test = InsertNewsofDiary(friendsID[i], diary);
            }
            return test;
        }


        /// <summary>
        /// 输入一个接受用户邮箱和一个活动，插入到新鲜事中去
        /// </summary>
        /// <param name="hostID">接收用户id</param>
        /// <param name="act">活动data</param>
        /// <returns></returns>
        public bool InsertNewsofActivity(String hostID, b_Activitydata act)
        {
            return (new b_Friends_News()).InsertNewsofActivity(hostID, act);
        }


        /// <summary>
        /// 确认一个新鲜事，即将这个新鲜事标记为已读
        /// </summary>
        /// <param name="newsdata"></param>
        /// <returns></returns>
        public bool ConfirmNews(b_Friends_Newsdata newsdata)
        {
            return (new b_Friends_News()).ConfirmNews(newsdata);
        }


        /// <summary>
        /// 通过用户id和日志id将该新鲜事标记为已读
        /// </summary>
        /// <param name="hostID"></param>
        /// <param name="diaryID"></param>
        /// <returns></returns>
        public bool ConfirmNewsByBoth(String hostID, String diaryID)
        {
            
            b_Friends_Newsdata friendsnewsdata = new b_Friends_Newsdata();
            friendsnewsdata = (new b_Friends_News()).getnewsbyboth(hostID, Int32.Parse(diaryID));
            return ConfirmNews(friendsnewsdata);
        }



        /// <summary>
        /// 删除一个新鲜事
        /// </summary>
        /// <param name="newsdata"></param>
        /// <returns></returns>
        public bool DeleteNews(b_Friends_Newsdata newsdata)
        {
            return (new b_Friends_News()).DeleteNews(newsdata);
        }


        /// <summary>
        /// 删除一个用户接受的所有新鲜事，成功返回true,失败返回false
        /// </summary>
        /// <param name="hostID"></param>
        /// <returns></returns>
        public bool DeleteAllNewsbyhostID(String hostID)
        {
            return (new b_Friends_News()).DeleteNewsbyhostname(hostID);
        }

        
        /// <summary>
        /// 删除一个用户发出的所有新鲜事
        /// </summary>
        /// <param name="guestID">guestID</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool DeleteNewsbyGuestID(String guestID)
        {
            return (new b_Friends_News()).DeleteNewsbyhostname(guestID);
        }

    }
}
